package com.bing.util;

import com.bing.beans.Person;
import com.bing.beans.WeiBo;
import com.bing.sina.TaoListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2014/6/29.
 */
public class TaoMath {
    public static Random random = new Random();
    public static final String DATE_FORMATE = "yyyy-MM-dd hh:mm:ss"; //被举报评论 发布时间：2014-08-03 11:37:02 | 评论原文
    public static final String DATE_YYYY_MM_dd_hh_mm = "yyyy-MM-dd hh:mm";
    public static final String DATE_YMDHM = "yyyy-MM-dd-hh-mm";

    public static final String DATE_MM_DD = "MM月dd日 hh:mm";

    public static final String ZHUCE_DATE_FORMATE = "yyyy-MM-dd"; //被举报评论 发布时间：2014-08-03 11:37:02 | 评论原文
    public static final SimpleDateFormat WEIBODATE2 = new SimpleDateFormat(DATE_MM_DD);
    public static final SimpleDateFormat WEIBODATE1 = new SimpleDateFormat(DATE_YYYY_MM_dd_hh_mm);
    public static final SimpleDateFormat SDF_DATE_YMDHM = new SimpleDateFormat(DATE_YMDHM);

    public static final SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMATE);
    public static final SimpleDateFormat sdfzhuce = new SimpleDateFormat(ZHUCE_DATE_FORMATE);
    public static Pattern guanZhu_fans_weibo = Pattern.compile("(?<guanZhu>\\d{1,}) 关注 (?<fenSi>\\d{1,}) 粉丝 (?<weiBo>\\d{1,}) 微博");
    public static Pattern patternDate = Pattern.compile("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}");
    public static Pattern patternCount = Pattern.compile("\\d{1,9}");
    public static Pattern patternBianHao = Pattern.compile("rid=(.*)");   //http://service.account.weibo.com/show?rid=K1CaL6gxi66kj
    public static Pattern patternInfo = Pattern.compile("(.*)\\s++(.*)");   // 昵称 \n 浙江洪燕华
    public static Pattern pattern_WEIBO_ATTR = Pattern.compile("(?<text>[\\s\\S]*)(\\((?<zan>\\d{1,9})\\))?\\| 转发(\\((?<zhuangfa>\\d{1,9})\\))?\\| 收藏(\\((?<shoucang>\\d{1,9})\\))?\\| 评论(\\((?<pinlun>\\d{1,9})\\))?.*((?<MDHS>\\d{1,2}月\\d{1,2}日 \\d{2}:\\d{2})|(?<YMDHS>\\d{4}-\\d{1,2}-\\d{1,2} \\d{2}:\\d{2})|前|(?<today>天 \\d{2}:\\d{2})).*"
    );

    public static Pattern pattern_WEIBO_JiFen = Pattern.compile("\"(?<daren>.*?)\\\\s*积分：(?<jifen>[\\\\d.]*)\"\n"); //白银达人    积分：4278.0 
    public static Pattern pattern_WEIBO_level = Pattern.compile("(?<level>\\d{1,9})级(?<days>\\d{1,9})天");//5级180天48天

    public static Pattern pattern_FENSI_ATTR = Pattern.compile("关注\\s(?<guanzhu>\\d{1,9})?\\s\\|\\s粉丝\\s(?<fensi>\\d{1,9})?\\s\\|\\s微博\\s(?<weibo>\\d{1,9})?");  //关注 1607 | 粉丝 1112 | 微博 216
    public static final Logger logger = LoggerFactory.getLogger(TaoMath.class);

    public static Pattern pattern_WeiBo_Zan_ZhuangFa = Pattern.compile("赞(\\((?<zan>\\d{1,9})\\))?\\| 转发(\\((?<zhuangfa>\\d{1,9})\\))?\\| 收藏(\\((?<shoucang>\\d{1,9})\\))?\\| 评论(\\((?<pinlun>\\d{1,9})\\))?"
    ); //赞| 转发(1)| 收藏| 评论
    public static Pattern pattern_WeiBo_URL = Pattern.compile(".*(?<=/)(?<url>.*$)");  //  http://weibo.com/u/2546215074
/*
    public static final Map<Integer, Integer> huoYue = new HashMap<Integer, Integer>

    {
        {
            huoYue.put(1, 3);
            huoYue.put(2, 12);
            huoYue.put(3, 33);
            huoYue.put(4, 72);
            huoYue.put(5, 135);
            huoYue.put(6, 228);
            huoYue.put(7, 357);
            huoYue.put(8, 528);
            huoYue.put(9, 747);
            huoYue.put(10, 1024);
            huoYue.put(11, 1353);
            huoYue.put(12, 1752);
            huoYue.put(13, 2223);
            huoYue.put(14, 2772);
            huoYue.put(15, 3405);
            huoYue.put(16, 4128);
            huoYue.put(17, 4947);
            huoYue.put(18, 5868);
            huoYue.put(19, 6897);
            huoYue.put(20, 8040);
        }
    };*/

    public static List<Integer> getRandoms(int max, int size) {
        Assert.isTrue(size > 0 && max >= (size - 1), "max 应该大于等于size-1. 并且size 大于0");
        List<Integer> ints = new ArrayList<Integer>();
        while (true) {
            ints.add(random.nextInt(max));
            if (ints.size() == size) {
                break;
            }
        }
        return ints;
    }

    public static Date toDate(String dateStr) {
        Date date = null;
        Matcher m = patternDate.matcher(dateStr);
        if (m.find()) {
            try {
                date = sdf.parse(m.group());
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            logger.error("ParseDate:{} failed.", dateStr);
        }
        return date;

    }


    public static int toBrowse(String browse) {
        Matcher m = patternCount.matcher(browse);
        if (m.find()) {
            return Integer.valueOf(m.group());
        } else {
            logger.error("ParseCount:{} failed.", browse);
        }
        return 0;
    }

    public static String toBianHao(String url) {
        Matcher m = patternBianHao.matcher(url);
        if (m.find()) {
            return m.group(1);
        } else {
            logger.error("ParseCount:{} failed.", url);
        }
        return null;
    }

    /*转发微博
    ◆
            ◆
    @沈阳足球网
    触目惊心【视频：哈尔滨毅腾主场对辽宁宏运中场休息】 （分享自 @优酷）
            |
    哈尔滨毅腾主场对辽宁宏运中场休息
            哈尔滨毅腾主场对辽宁宏运中场休息
    播放
    3
            | 转发(2) | 评论
    今天 14:36来自优酷网连接分享
    | 转发| 收藏(333)| 评论(455)
    今天 14:42 来自小米手机2*/

    public static void setPersonRzJiFen(Person p, String text) {
        Matcher m = pattern_WEIBO_JiFen.matcher(text);
        if (m.find()) {
            String daRen = m.group("daren");
            String jiFen = m.group("jifen").trim();
            p.setRenZheng(daRen);
            p.setJiFen(Double.valueOf(jiFen));
            logger.debug("url:{},达人:{},积分:", p.getUrl(), daRen, jiFen);
        }
    }

    public static WeiBo toWeiBo(String text) {
        WeiBo weiBo = new WeiBo();
        if (text.startsWith("|")) {
            text = "\n" + text;
        }
        Matcher m = pattern_WEIBO_ATTR.matcher(text);
        if (m.find()) {
            String content = m.group("text");
            weiBo.setContent(content);
            String shoucang = m.group("shoucang");
            String zhuangfa = m.group("zhuangfa");
            String pinlun = m.group("pinlun");
            String zan = m.group("zan");
            weiBo.setPingLun(pinlun == null ? 0 : Integer.valueOf(pinlun));
            weiBo.setZhuangFa(zhuangfa == null ? 0 : Integer.valueOf(zhuangfa));
            weiBo.setZan(zan == null ? 0 : Integer.valueOf(zan));
            weiBo.setShouCang(shoucang == null ? 0 : Integer.valueOf(shoucang));
            String YMDHS = m.group("YMDHS");
            String MDHS = m.group("MDHS");
            logger.debug("微博:{},收藏:{},转发:{},评论:{},赞:{},YMDHS:{},MDHS:{}", content, shoucang, zhuangfa, pinlun, zan, YMDHS, MDHS);
            try {
                if (YMDHS != null) {
                    weiBo.setCreate_date(WEIBODATE1.parse(YMDHS));

                } else if (MDHS != null) {
                    Date date = WEIBODATE2.parse(MDHS);
                    Calendar c = Calendar.getInstance();
                    c.setTime(date);
                    c.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR));
                    weiBo.setCreate_date(c.getTime());
                } else {
                    weiBo.setCreate_date(new Date());
                }
            } catch (ParseException e) {
                weiBo.setCreate_date(new Date());
                e.printStackTrace();
            }
        }
        logger.info("text: {}, 微薄:{}", text, weiBo.toString());
        return weiBo;
    }

    public static void setLevel(Person p, String levelInfo) {
        Matcher m = pattern_WEIBO_level.matcher(levelInfo);
        if (m.find()) {
            String level = m.group("level");
            String days = m.group("days");
            logger.debug("等级信息:{},level:{},days:{}", levelInfo, level, days);
            p.setDengJi(Integer.valueOf(level));
            p.setHuoYue(Integer.valueOf(days));
        }
    }

    /**
     * 提取关注,粉丝,微波
     *
     * @param person
     * @param attr   关注 1607 | 粉丝 1112 | 微博 216
     */
    public static void setPersonAttr(Person person, String attr) {
        Matcher m = pattern_FENSI_ATTR.matcher(attr);
        if (m.find()) {
            String guanZhu = m.group("guanzhu");
            String fensi = m.group("fensi");
            String weibo = m.group("weibo");
            logger.debug("采集粉丝属性:{}, text:{}, 关注:{},粉丝:{},微波:{}", person.getUrl(), attr, guanZhu, fensi, weibo);
            person.setFenSi(fensi == null ? 0 : Integer.valueOf(fensi));
            person.setWeiBoShu(weibo == null ? 0 : Integer.valueOf(weibo));
            person.setGuanZhu(guanZhu == null ? 0 : Integer.valueOf(guanZhu));
        } else {
            logger.error("采集粉丝属性失败:{}", attr);
        }
    }

    public static void setGuanZhu_fans_weibo(Person person, String text) {
        Matcher m = guanZhu_fans_weibo.matcher(text);
        if (m.find()) {
            String guanZhu = m.group("guanZhu");
            String fensi = m.group("fenSi");
            String weibo = m.group("weiBo");
            logger.debug("采集粉丝属性:{}, text:{}, 关注:{},粉丝:{},微波:{}", person.getUrl(), text, guanZhu, fensi, weibo);
            person.setFenSi(fensi == null ? 0 : Integer.valueOf(fensi));
            person.setWeiBoShu(weibo == null ? 0 : Integer.valueOf(weibo));
            person.setGuanZhu(guanZhu == null ? 0 : Integer.valueOf(guanZhu));
            Statistics.t_weiBo.set(Integer.valueOf(weibo));
            Statistics.t_fenSi.set(fensi == null ? 0 : Integer.valueOf(fensi));
            Statistics.t_guanZhu.set(guanZhu == null ? 0 : Integer.valueOf(guanZhu));
        } else {
            logger.error("采集粉丝属性失败:{}", text);
        }
    }

    public static String getWeiBoPersonUrl(String url) {
        Matcher m = pattern_WeiBo_URL.matcher(url);
        String personUrl = null;
        if (m.find()) {
            personUrl = m.group("url");
            logger.debug("转换personUrl :{}", personUrl);
        } else {
            logger.error("转换personUrl 失败:{}", url);
        }
        return personUrl;
    }

    /**
     * 赞| 转发(1)| 收藏| 评论   提取数字
     * @param weiBo
     * @param zanZhangFa
     */
    public static void setZanZhuangFa(WeiBo weiBo, String zanZhangFa) {
        Matcher m = pattern_WeiBo_Zan_ZhuangFa.matcher(zanZhangFa);
        if (m.find()) {
            String zan = m.group("zan");
            String zhuangFa = m.group("zhuangfa");
            String shouCang = m.group("shoucang");
            String pinlun = m.group("pinlun");
            logger.info("采集微薄属性:{}, 赞:{}, 转发:{},收藏:{},评论:{}",zanZhangFa,zan,zhuangFa,shouCang,pinlun);
            weiBo.setZan(zan == null ? 0 : Integer.valueOf(zan));
            weiBo.setZhuangFa(zhuangFa == null ? 0 : Integer.valueOf(zhuangFa));
            weiBo.setShouCang(shouCang == null ? 0 : Integer.valueOf(shouCang));
            weiBo.setPingLun(pinlun == null ? 0 : Integer.valueOf(pinlun));
        } else {
            logger.error("采集粉丝属性失败:{}", zanZhangFa);
        }
    }
}
